import Vue from 'vue';
import Vuex from 'vuex';
import allRoutes from '@/router/allRoutes'
import recursionRoutes from '@/utils/recursionRoutes'
import dynamicRoutes from '@/router/dynamicRoutes'
Vue.use(Vuex);
import * as api from "../api"
import router from '../router';

export default new Vuex.Store({
    state:{
        userInfo:JSON.parse(localStorage.getItem("qf-userInfo")) || "{}",
        sideMenu:[],
        breadCrumb:[],
        dialogVisible:false,
        dialogTitle:"增加学员信息",
        stuData:[],
        currentRow:{}
    },
    actions:{
      async GET_MENULIST({commit}){
          let res = await api.getMenuListApi();
          let { menuList } = res.data;
          let userRoutes = recursionRoutes(menuList,allRoutes)
          commit("SET_MENULIST",userRoutes)
      },
      GET_STULIST({commit},payload={}){
          let{page=1,count=15,classes=""} = payload
          api.getStuListApi(page,count,classes)
          .then(res =>{
              commit("SET_STU_DATA",res.data.data)
          })
      }
    },
    mutations:{
        SET_STU_DATA(state,payload){
           state.stuData = payload
        },
        SHOW_DIALOG(state){
        state.dialogVisible = true
        },
        HIDE_DIALOG(state){
        state.dialogVisible = false
        },
        CHANGE_DIALOG_TITLE(state,payload){
            state.dialogTitle = payload
        },
        SET_USER_INFO(state,payload){
           state.userInfo = payload;
        },
        SET_MENULIST(state,payload){
            state.sideMenu = payload;
            let target = dynamicRoutes.find(item =>item.path === "/")
            target.children = payload
            router.addRoutes(dynamicRoutes)
        },
        SET_BREADCRUMB(state,payload){
            state.breadCrumb = payload
        },
        SET_CURRENT_ROW(state,payload){
          state.currentRow = {...payload}
        }


    },
    getters:{

    },
    modules:{

    }
})